namespace effect {



	/**
	 * 移除偏色效果（多用于控件效果复原）
	 * @param obj 目标对象
	 * @description 该方法会移除所有已施加的toLight/toGray/toColor方法
     * @return 对象自身
	 */
	export function removeColor<T extends egret.DisplayObject>(obj: T): T {
		if (notEmptyArr(obj.filters)) {
			let tempArr = [];
			for (let i = 0; i < obj.filters.length; i++) {
				if (obj.filters[i] instanceof egret.ColorMatrixFilter) continue;
				tempArr.push(obj.filters[i]);
			}
			obj.filters = tempArr;
		}
		return obj;
	}

	/**
	 * 变亮效果（多用于控件点击响应）
 	 * @param obj 目标对象
	 * @description 该方法会移除所有已施加的toGray/toColor方法
  	 * @return 对象自身
	 */
	export function toLight<T extends egret.DisplayObject>(obj: T): T {
		let filter = new egret.ColorMatrixFilter(Color.Default.LIGHT_MATRIX);
		removeColor(obj);
		if (isArray(obj.filters)) obj.filters = obj.filters.concat(filter);
		else obj.filters = [filter];
		return obj;
	}

	/**
	 * 变灰效果（多用于控件被设置无效）
 	 * @param obj 目标对象
	 * @description 该方法会移除所有已施加的toLight/toColor方法
  	 * @return 对象自身
	 */
	export function toGray<T extends egret.DisplayObject>(obj: T): T {
		let filter = new egret.ColorMatrixFilter(Color.Default.GRAY_MATRIX);
		removeColor(obj);
		if (isArray(obj.filters)) obj.filters = obj.filters.concat(filter);
		else obj.filters = [filter];
		return obj;
	}

	/**
	 * 切换效果（多用于可切换状态控件）
 	 * @param obj 目标对象
	 * @param down 是否被按下
	 * @param modes 切换模式罗列
  	 * @return 对象自身
	 */
	export function toToggle<T extends egret.DisplayObject>(obj: T, selected: boolean, ...modes: hjc.ToggleMode[]): T {
		if (notEmptyArr(modes)) {
			if (modes.contains(hjc.ToggleMode.ALPHA_CHG)) obj.alpha = selected ? 1 : game.toggledAlpha;
			if (modes.contains(hjc.ToggleMode.SCALE_CHG)) ui.setScale(obj, selected ? game.toggledScale : 1);
		}
		return obj;
	}

	export function toShowPart<T extends egret.DisplayObject>(obj: T, options: hjc.AnimationOptions, check: boolean = true): T {
		if (!obj) { hjc.warn(`display object is empty!`); return obj; }
		if (!!check) {
			options.end = utils.num(options.end, 1, 0, 1);
			options.start = utils.num(options.start, 1, 0, options.end);
			options.direction = !!options.direction ? options.direction : eui.Direction.LTR;
		}
		if (options.start <= 0 && options.end >= 1) obj.mask = null;
		else {
			let [x, y, w, h, diff] = [0, 0, obj.width, obj.height, options.end - options.start];
			switch (options.direction) {
				case eui.Direction.LTR: { x = w * options.start; w = w * diff; } break;
				case eui.Direction.RTL: { x = w * (1 - options.end); w = w * diff; } break;
				case eui.Direction.TTB: { y = h * options.start; h = h * diff; } break;
				case eui.Direction.BTT: { y = h * (1 - options.end); h = obj.height * diff; } break;
				default: hjc.warn(`invalid Direction: ${options.direction}`);
			}
			if (obj.mask) {
				obj.mask.x = x; obj.mask.width = w;
				obj.mask.y = y; obj.mask.height = h;
				obj.mask = obj.mask;
			} else obj.mask = new egret.Rectangle(x, y, w, h);
		}
		return obj;
	}

	/**
	 * 发光效果（多用于控件被设置无效）
	 * @param obj 目标对象
	 * @param color 光泽颜色 *默认正黄色
	 * @param glowMode 发光强度 *默认正常
	 * @return 对象自身
	 */
	export function toGlow<T extends egret.DisplayObject>(obj: T, color: number = Color.YELLOW, glowMode: number = hjc.GlowDegree.NORMAL): void {
		let [clr, alpha, blurX, blurY] = [color, 0.7, 4, 4];
		let strength = 1;                           //印压强度
		let quality = egret.BitmapFilterQuality.LOW;//应用次数
		let inner = false;                          //内侧光晕
		let knockout = false;                       //挖空效果
		if (glowMode === hjc.GlowDegree.SLIGHT) {
			alpha = 0.3; blurX = 2; blurY = 2; strength = 1;
		} else if (glowMode === hjc.GlowDegree.STRONG) {
			alpha = 0.9; blurX = 10; blurY = 10; strength = 2;
		}
		let glowFilter: egret.GlowFilter = new egret.GlowFilter(clr, alpha, blurX, blurY, strength, quality, inner, knockout);
		if (Array.isArray(obj.filters)) {
			obj.filters = obj.filters.concat(glowFilter);
		} else obj.filters = [glowFilter];
	}

	/**
	 * 移除发光滤镜
	 * @param obj 目标对象
	 * @return 对象自身
	 */
	export function removeGlow<T extends egret.DisplayObject>(obj: T): T {
		if (notEmptyArr(obj.filters)) {
			let tempArr = [];
			for (let i = 0; i < obj.filters.length; i++) {
				if (obj.filters[i] instanceof egret.GlowFilter) continue;
				tempArr.push(obj.filters[i]);
			}
			obj.filters = tempArr;
		}
		return obj;
	}

	export function toColor<T extends egret.DisplayObject>(obj: T, color: color): T {
		if (!isColor(color)) { hjc.warn(`param ${color} isn't a valid color!`); return; }
		removeColor(obj);
		let matrix = Color.Default.COLOR_MATRIX.concat([]);
		let {r, g, b} = Color.toRGB(color);
		matrix[4] = r; matrix[9] = g; matrix[14] = b;
		let filter = new egret.ColorMatrixFilter(matrix);
		_addFilter(obj, filter);
		return obj;
	}

	function _addFilter(obj: egret.DisplayObject, filter: egret.Filter): void {
		obj.filters = isArray(obj.filters) ? obj.filters.concat(filter) : [filter];
	}
}